HTTPS就等于HTTP加上TLS(SSL),HTTPS协议的目标主要有三个:
数据保密性。保证内容在传输过程中不会被第三方查看到。就像快递员传递包裹时都进行了封装,别人无法知道里面装了什么东西。
数据完整性。及时发现被第三方篡改的传输内容。就像快递员虽然不知道包裹里装了什么东西,但他有可能中途掉包,数据完整性就是指如果被掉包,我们能轻松发现并拒收。
身份校验。保证数据到达用户期望的目的地。就像我们邮寄包裹时,虽然是一个封装好的未掉包的包裹,但必须确定这个包裹不会送错地方。
关于申请SSL及在站点部署请看下面两篇博文:
StartSSL免费SSL证书申请和账户注册完整过程
新StartSSL免费SSL证书申请使用:Apache和Ngnix安装配置SSL证书
Nginx下配置网站ssl实现https访问
HTTPS介绍
Nginx配置SSL证书
申请的过程可以看上边的第一篇博文,申请后最终会需要两个文件,一个扩展名为.crt,一个扩展名为.key文件,然后通过命令将其传入nginx服务器的目录下:
1.将本地文件上传到服务器的/home/目录下先
➜ ~ pwd
/Users/corwien
// 将本地文件上传到服务器的/home/目录下先
scp /Users/corwien/ssl/ssl.domain—name.cn.crt root@120.23.12.5:/home/
scp /Users/corwien/ssl/ssl.domain-name.cn.key root@120.23.12.5:/home/
2.打开站点的配置文件
vim /etc/nginx/sites-available/default
下面是我添加SSL后的配置文件,需要的可以参考:
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
root /var/www/your-project/public;
index index.php index.html index.htm;
# Make site accessible from http://localhost/
server_name your-domain.cn;
# SSL重写指向下面的HTTPS的443端口-20160924
rewrite ^/(.*) https://your-domain.cn/$1 permanent;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ /index.php?$query_string;
# Uncomment to enable naxsi on this location
}
location ~ \.php$ {
try_files $uri /index.php =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
# HTTPS server
#
server {
listen 443;
server_name your-domain.cn;
#
root /var/www/your-domain/public;
index index.php index.html index.htm;
# SSL 配置
ssl on;
ssl_certificate /home/ssl.your-domain.cn.crt;
ssl_certificate_key /home/ssl.your-domain.cn.key;
#
ssl_session_timeout 5m;
#
# ssl_protocols SSLv3 TLSv1 TLSv1.1 TLSv1.2;
# ssl_ciphers "HIGH:!aNULL:!MD5 or HIGH:!aNULL:!MD5:!3DES";
# ssl_prefer_server_ciphers on;
#
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
try_files $uri /index.php =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
3.保存,重启nginx
service nginx restart
这时会出现这样的提示,让你输入你的SSL证书的密码(就是你生成cer那一步的密码,可别忘记了)
* Restarting nginx nginx
Enter PEM pass phrase: your_ssl_password(输入你的密码)
Enter PEM pass phrase: your_ssl_password(输入你的密码)
[ OK ]
在浏览器输入你的网站域名:
https://your-domian.cn
OK ,如果不出意外,你的SSL配置成功了!
修改站点中非https的资源链接
HTTPS的优缺点
既然HTTPS非常安全,数字证书费用也不高,那为什么互联网公司不全部使用HTTPS呢?原因主要有两点:
HTTPS对速度的影响非常明显。每个HTTPS连接一般会增加1-3个RTT,加上加解密对性能的消耗,延时还有可能再增加几十毫秒。
HTTPS对CPU计算能力的消耗很严重,完全握手时,web server的处理能力会降低至HTTP的10%甚至以下。
HTTPS为什么会严重降低性能?主要是握手阶段时的大数运算。其中最消耗性能的又是密钥交换时的私钥解密阶段(函数是rsa_private_decryption)。这个阶段的性能消耗占整个SSL握手性能消耗的95%。
然而随着各大网站的相继跟进与硬件的摩尔定律下,为了安全而做这点性能牺牲还是值得的。
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。